/**
 * Copyright (C) 2012 Shindo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "a_samp.h"
#include "Common.h"
#include <stack>
using namespace std;

#pragma warning (push)
#pragma warning (disable: 4996)
void QueryFileFromFolder(LPSTR szPath, LPSTR szExt, IQueryFileCallBack lpFuncCallBack, bool bRecursion)
{
	stack<LPSTR> stackDir;
	LPSTR szTopPath = new CHAR[strlen(szPath) + 10];
	wsprintf(szTopPath, "%s\\*.*", szPath);
	stackDir.push(szTopPath);

	while (!stackDir.empty())
	{
		LPSTR szNow = stackDir.top();
		stackDir.pop();

		WIN32_FIND_DATA findData;
		HANDLE hFindFile = FindFirstFile(szNow, &findData);
		if (hFindFile != INVALID_HANDLE_VALUE)
		{
			do
			{
				if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				{
					if (!bRecursion) continue;

					if (strcmp(findData.cFileName, ".") && strcmp(findData.cFileName, ".."))
					{
						LPSTR szToAddPath = new CHAR[strlen(szNow) + 1];
						strcpy(szToAddPath, szNow);
						szToAddPath[strlen(szToAddPath) - 4] = NULL;

						LPSTR szNextPath = new CHAR[strlen(szToAddPath) + strlen(findData.cFileName) + 15];
						wsprintf(szNextPath, "%s\\%s\\*.*", szToAddPath, findData.cFileName);
						stackDir.push(szNextPath);

						LPSTR szToBack = new CHAR[strlen(szToAddPath) + strlen(findData.cFileName) + 15];
						wsprintf(szToBack, "%s\\%s", szToAddPath, findData.cFileName);

						lpFuncCallBack(szToBack, true, -1);

						delete[] szToAddPath;
						delete[] szToBack;
					}
				}
				else
				{
					CHAR *dotpos = strrchr(findData.cFileName, '.');
					if (dotpos && !strcmp(dotpos, szExt))
					{
						LPSTR szToAddPath = new CHAR[strlen(szNow) + 1];
						strcpy(szToAddPath, szNow);
						szToAddPath[strlen(szToAddPath) - 4] = NULL;

						LPSTR szName = new CHAR[strlen(szToAddPath) + strlen(findData.cFileName) + 10];
						wsprintf(szName, "%s\\%s", szToAddPath, findData.cFileName);

						delete[] szToAddPath;

						lpFuncCallBack(szName, false, -1);

						delete[] szName;
					}
				}
			} while (FindNextFile(hFindFile, &findData));
		}
		FindClose(hFindFile);

		delete[] szNow;
	}

	while (!stackDir.empty())
	{
		LPSTR szTmp = stackDir.top();
		stackDir.pop();
		delete[] szTmp;
	}
}
#pragma warning (pop)

vector<string> stringSplit(string source, char splitChar)
{
	vector<string> ret;
	while (source.find(splitChar) != string::npos)
	{
		ret.push_back(source.substr(0, source.find(splitChar)));
		source = source.substr(source.find(splitChar) + 1, source.length() - source.find(splitChar));
	}
	ret.push_back(source);

	return ret;
}

BOOL WINAPI WritePrivateProfileInt(LPCSTR lpAppName, LPCSTR lpKeyName, int nValue, LPCSTR lpFileName)
{
	stringstream streamWrite;
	streamWrite<<nValue;
	
	return WritePrivateProfileString(lpAppName, lpKeyName, streamWrite.str().c_str(), lpFileName);
}

bool isFilenameValid(string filename)
{
	char SpecialChar[] = {'/','\\','\"',':','*','?','<','>','|','\t'};
	for (int i = 0; i < sizeof(SpecialChar) / sizeof(char); i++)
	{
		if (string::npos != filename.find(SpecialChar[i]))
			return false;
	}

	return true;
}